<?php
class Kuesioner_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
	
	function insert_kuesioner($nama_kuesioner, $id_user, $deskripsi, $proposal, $link, $publish_penelitian, $publish_metodologi, $publish_data)
	{
		$data = array(
			'nama_kuesioner' => $nama_kuesioner,
			'fk_id_user' => $id_user,
			'deskripsi' => $deskripsi,
			'file_proposal' => $proposal,
			'link' => $link,
			'publish' => $publish_penelitian,
			'publish_metodologi' => $publish_metodologi,
			'publish_data' => $publish_data
		);
		
		$this->db->insert('"kuesioner"', $data);
		return $this->db->insert_id();
	}
	
	function update_kuesioner($nama, $deskripsi, $id_kues)
	{
		$data = array(
			'nama_kuesioner' => $nama,
			'deskripsi' => $deskripsi
		);
		$this->db->where('id_kuesioner', $id_kues);
		$this->db->update('"kuesioner"', $data);
	}
	
	function update_file($file_proposal, $id_kues)
	{
		$data = array(
			'file_proposal' => $file_proposal,
		);
		$this->db->where('id_kuesioner', $id_kues);
		$this->db->update('"kuesioner"', $data);
	}
	
	function insert_pertanyaan_teks($pertanyaan, $id_kuesioner, $id_jenis_pertanyaan, $jenis_identitas)
	{
		$data = array(
			'pertanyaan' => $pertanyaan,
			'fk_id_kuesioner' => $id_kuesioner,
			'fk_id_jenis_pertanyaan' => $id_jenis_pertanyaan,
			'fk_id_jenis_teks' => $jenis_identitas,
			);
			
		$this->db->insert('"pertanyaan"', $data);
	}
	
	function insert_skala($skala)
	{
		$data = array(
			'skala' => $skala
			);
		$this->db->insert('"skala"', $data);
		return $this->db->insert_id();
	}
	
	function update_skala($skala, $id_skala)
	{
		$data = array(
			'skala' => $skala
			);
		$this->db->where('id_skala', $id_skala);
		$this->db->update('"skala"', $data);
	
	}
	
	function insert_pertanyaan_skala($pertanyaan, $id_kuesioner, $id_jenis_pertanyaan, $id_skala)
	{
		$data = array(
			'pertanyaan' => $pertanyaan,
			'fk_id_kuesioner' => $id_kuesioner,
			'fk_id_jenis_pertanyaan' => $id_jenis_pertanyaan,
			'fk_id_skala' => $id_skala
			);
			
		$this->db->insert('"pertanyaan"', $data);
	}
	
	function insert_pertanyaan_pilihan($pertanyaan, $id_kuesioner, $id_jenis_pertanyaan, $id_pilihan)
	{
		$data = array(
			'pertanyaan' => $pertanyaan,
			'fk_id_kuesioner' => $id_kuesioner,
			'fk_id_jenis_pertanyaan' => $id_jenis_pertanyaan,
			'fk_id_pilihan' => $id_pilihan
			);
			
		$this->db->insert('"pertanyaan"', $data);
	}
	
	function insert_pilihan($skala)
	{
		$data = array(
			'pilihan' => $skala
			);
		$this->db->insert('"pilihan_ganda"', $data);
		return $this->db->insert_id();
	}
	
	function update_pertanyaan_lama($pertanyaan, $id_pertanyaan)
	{
		$data = array(
			'pertanyaan' => $pertanyaan,
			);
		$this->db->where('id_pertanyaan', $id_pertanyaan);
		$this->db->update('"pertanyaan"', $data);
	}
	
	function update_kuesskala_lama($pertanyaan, $id_pertanyaan, $id_skala)
	{
		$data = array(
			'pertanyaan' => $pertanyaan,
			'fk_id_skala' => $id_skala
			);
		$this->db->where('id_pertanyaan', $id_pertanyaan);
		$this->db->update('"pertanyaan"', $data);
	}
	
	function update_pg($pilihan, $id_pilihan)
	{
		$data = array(
			'pilihan' => $pilihan
			);
		$this->db->where('id_pilihan_ganda', $id_pilihan);
		$this->db->update('"pilihan_ganda"', $data);
	}
	
	function get_kuesioner_by_iduser($id_user)
	{
		$this->db->select('*');
		$this->db->from('kuesioner');
		$this->db->where('fk_id_user', $id_user);
		$this->db->where('status', 0);
		$result = $this->db->get();
		return $result->result_array();
	}
	
	function get_data_by_iduser($id_user)
	{
		$this->db->select('*');
		$this->db->from('kuesioner');
		$this->db->where('fk_id_user', $id_user);
		$result = $this->db->get();
		return $result->result_array();
	}
	
	function get_kuesioner($id_kuesioner)
	{
		$this->db->select('*');
		$this->db->from('kuesioner');
		$this->db->where('id_kuesioner', $id_kuesioner);
		$result = $this->db->get();
		return $result->result_array();
	}
	
	function get_all_kuesioner()
	{
		$this->db->select('*');
		$this->db->from('kuesioner');
		$this->db->where('publish', 1);
		$result = $this->db->get();
		return $result->result_array();
	}
	
	function get_konten_teks($id_kuesioner)
	{
		$this->db->select('*');
		$this->db->from('pertanyaan as per');
		$this->db->join('jenis_pertanyaan as jns', 'per.fk_id_jenis_pertanyaan = jns.id_jenis_pertanyaan');
		$this->db->join('skala as sk', 'per.fk_id_skala = sk.id_skala', 'left');
		$this->db->where('per.fk_id_kuesioner', $id_kuesioner);
		$this->db->where('per.fk_id_jenis_pertanyaan', 1);
		$result =  $this->db->get();
		return $result->result_array();
	}
	
	function get_konten_skala($id_kuesioner)
	{
		$this->db->select('*');
		$this->db->from('pertanyaan as per');
		$this->db->join('jenis_pertanyaan as jns', 'per.fk_id_jenis_pertanyaan = jns.id_jenis_pertanyaan');
		$this->db->join('skala as sk', 'per.fk_id_skala = sk.id_skala', 'left');
		$this->db->where('per.fk_id_kuesioner', $id_kuesioner);
		$this->db->where('per.fk_id_jenis_pertanyaan', 3);
		$result =  $this->db->get();
		return $result->result_array();
	}
	
	function get_konten_pg($id_kuesioner)
	{
		$this->db->select('*');
		$this->db->from('pertanyaan as per');
		$this->db->join('jenis_pertanyaan as jns', 'per.fk_id_jenis_pertanyaan = jns.id_jenis_pertanyaan');
		$this->db->join('pilihan_ganda as pg', 'per.fk_id_pilihan = pg.id_pilihan_ganda', 'left');
		$this->db->where('per.fk_id_kuesioner', $id_kuesioner);
		$this->db->where('per.fk_id_jenis_pertanyaan', 2);
		$result =  $this->db->get();
		return $result->result_array();
	}
	
	function get_kuesioner_teks($id_kuesioner)
	{
		$this->db->select('*');
		$this->db->from('pertanyaan as per');
		$this->db->join('jenis_pertanyaan as jns', 'per.fk_id_jenis_pertanyaan = jns.id_jenis_pertanyaan');
		$this->db->join('skala as sk', 'per.fk_id_skala = sk.id_skala', 'left');
		$this->db->where('per.fk_id_kuesioner', $id_kuesioner);
		$this->db->where('jns.id_jenis_pertanyaan', 1);
		$result =  $this->db->get();
		return $result->result_array();
	}
	
	function get_kuesioner_skala($id_kuesioner)
	{
		$this->db->select('*');
		$this->db->from('pertanyaan as per');
		$this->db->join('jenis_pertanyaan as jns', 'per.fk_id_jenis_pertanyaan = jns.id_jenis_pertanyaan');
		$this->db->join('skala as sk', 'per.fk_id_skala = sk.id_skala', 'left');
		$this->db->where('per.fk_id_kuesioner', $id_kuesioner);
		$this->db->where('jns.id_jenis_pertanyaan', 3);
		$result =  $this->db->get();
		return $result->result_array();
	}
	
	function get_kuesioner_pg($id_kuesioner)
	{
		$this->db->select('*');
		$this->db->from('pertanyaan as per');
		$this->db->join('jenis_pertanyaan as jns', 'per.fk_id_jenis_pertanyaan = jns.id_jenis_pertanyaan');
		$this->db->join('pilihan_ganda as pg', 'per.fk_id_pilihan = pg.id_pilihan_ganda', 'left');
		$this->db->where('per.fk_id_kuesioner', $id_kuesioner);
		$this->db->where('jns.id_jenis_pertanyaan', 2);
		$result =  $this->db->get();
		return $result->result_array();
	}
	
	function get_skala($id_kuesioner)
	{
		$this->db->select('*');
		$this->db->from('pertanyaan as per');
		$this->db->join('jenis_pertanyaan as jns', 'per.fk_id_jenis_pertanyaan = jns.id_jenis_pertanyaan');
		$this->db->join('skala as sk', 'per.fk_id_skala = sk.id_skala', 'left');
		$this->db->where('per.fk_id_kuesioner', $id_kuesioner);
		$this->db->where('jns.id_jenis_pertanyaan', 3);
		$result =  $this->db->get();
		return $result->row_array();
	}
	
	function insert_responden($user)
	{
		$data = array(
			'responden' => $user
		);
		
		$this->db->insert('responden', $data);
		return $this->db->insert_id();
	}
	
	function insert_jawaban_responden($jawaban)
	{
		$data = array(
			'jawaban' => $jawaban
		);
		$this->db->insert('jawaban_responden', $data);
		return $this->db->insert_id();
	}
	
	function insert_responden_kuesioner($id_responden, $id_pertanyaan, $id_jawaban)
	{
		$data = array(
			'fk_id_responden' => $id_responden,
			'fk_id_pertanyaan' => $id_pertanyaan,
			'fk_id_jawaban_responden' => $id_jawaban
		);
		$this->db->insert('responden_kuesioner', $data);		
	}
	
	function get_jawaban($id_responden)
	{
		$this->db->select('*');
		$this->db->from('responden_kuesioner as rk');
		$this->db->join('jawaban_responden as jr', 'jr.id_jawaban_responden = rk.fk_id_jawaban_responden');
		$this->db->where('rk.fk_id_responden', $id_responden);
		$result = $this->db->get();
		return $result->result_array();
	}
	
	function get_pertanyaan($id_kuesioner)
	{
		$this->db->select('*');
		$this->db->from('pertanyaan');
		$this->db->where('fk_id_kuesioner', $id_kuesioner);
		$this->db->order_by('fk_id_jenis_pertanyaan', 'asc');
		$this->db->order_by('id_pertanyaan', 'asc');
		$result = $this->db->get();
		return $result->result_array();
	}
	
	function responden($id_kuesioner)
	{
		$this->db->select('distinct rk.fk_id_responden');
		$this->db->from('responden_kuesioner as rk');
		$this->db->join('pertanyaan as pert', 'rk.fk_id_pertanyaan = pert.id_pertanyaan');
		$this->db->where('pert.fk_id_kuesioner', $id_kuesioner);
		$result = $this->db->get();
		return $result->result_array();
	}
	
	function delete_pertanyaan($id_pertanyaan)
	{
		$this->db->where('id_pertanyaan', $id_pertanyaan);
		$this->db->delete('pertanyaan');
	}
	
	function delete_skala($id_skala)
	{
		$this->db->where('id_skala', $id_skala);
		$this->db->delete('skala');
	}
	
	function delete_pg($id_pilihan)
	{
		$this->db->where('id_pilihan_ganda', $id_pilihan);
		$this->db->delete('pilihan_ganda');
	}
	
	function get_responden_kuesioner_byidpertanyaan($id_pertanyaan)
	{
		$this->db->select('fk_id_responden');
		$this->db->from('responden_kuesioner');
		$this->db->where('fk_id_pertanyaan', $id_pertanyaan);
		$result = $this->db->get();
		return $result->result_array();
	}
	
	function get_id_pertanyaan($id_kuesioner)
	{
		$this->db->select('*');
		$this->db->from('pertanyaan');
		$this->db->where('fk_id_kuesioner', $id_kuesioner);
		$result = $this->db->get();
		return $result->result_array();
	}
	
	function get_fk_id_responden($id_pertanyaan)
	{
		$this->db->distinct();
		$this->db->select('fk_id_responden');
		$this->db->from('"responden_kuesioner"');
		$this->db->where('fk_id_pertanyaan', $id_pertanyaan);
		$result = $this->db->get();
		return $result->result_array();
	}
		
	function delete_pilihan($id_pilihan)
	{
		$this->db->where('id_pilihan_ganda', $id_pilihan);
		$this->db->delete('pilihan_ganda');
	}
	
	function delete_kuesioner($id_kuesioner)
	{
		$this->db->where('id_kuesioner', $id_kuesioner);
		$this->db->delete('kuesioner');
	}
}
?>